package com.zyh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyh.entity.Attr;
import com.zyh.entity.ResultEntity;
import com.zyh.service.IAttrService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.PUT;
import java.util.List;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-05-11
 */
@RestController
@RequestMapping("/attr")
public class AttrController {
    @Autowired
    IAttrService attrService;

    //根据属性分组id查询该分组下面的属性信息
    @GetMapping("/selectAttrByGroupId/{groupId}")
    public ResultEntity<List<Attr>> selectAttrByGroupId(@PathVariable("groupId") Integer groupId){
        return ResultEntity.ok(attrService.list(new QueryWrapper<Attr>().eq("attr_group_id",groupId)));
    }

    //根据分类的id查询该分类下面的所有基本属性信息
    @GetMapping("/selectCommonAttrByGroupId/{groupId}")
    public ResultEntity<List<Attr>> selectCommonAttrByGroupId(@PathVariable("groupId") Integer groupId){
        return ResultEntity.ok(attrService.list(new QueryWrapper<Attr>().eq("attr_group_id",groupId).eq("attr_type",0)));
    }

    //添加基本属性
    @PostMapping("/addCommonAttr")
    public ResultEntity addCommonAttr(@RequestBody Attr attr){
        attr.setAttrType(0);
        return attrService.save(attr)==true ? ResultEntity.ok() : ResultEntity.error("添加失败！");
    }

    //删除属性
    @DeleteMapping("/delete/{attrId}")
    public ResultEntity delete(@PathVariable("attrId") Long attrId){
        return attrService.removeById(attrId)==true ? ResultEntity.ok() : ResultEntity.error("删除失败！");
    }

    //根据属性Id查询属性信息
    @GetMapping("/selectOne/{attrId}")
    public ResultEntity<Attr> selectOne(@PathVariable("attrId") Long attrId){
        return ResultEntity.ok(attrService.getById(attrId));
    }

    //修改属性信息
    @PutMapping("/update")
    public ResultEntity update(@RequestBody Attr attr){
        return ResultEntity.ok(attrService.updateById(attr));
    }

    //根据分类的id查询该分类下面的所有销售属性信息
    @GetMapping("/selectSaleAttrByGroupId/{groupId}")
    public ResultEntity<List<Attr>> selectSaleAttrByGroupId(@PathVariable("groupId") Integer groupId){
        return ResultEntity.ok(attrService.list(new QueryWrapper<Attr>().eq("attr_type",1).eq("attr_group_id",groupId)));
    }

    //添加销售属性
    @PostMapping("/addSaleAttr")
    public ResultEntity addSaleAttr(@RequestBody Attr attr){
        attr.setAttrType(1);
        return ResultEntity.ok(attrService.save(attr));
    }
}

